package cn._2dland.uploader.utils
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.formatters.DateFormatter;

	public final class Logger
	{
		private static var _inited:Boolean = false;
		private static var _stream:FileStream = null;

		public function Logger() {}

		public static function init():void {
			try {
				var logFile:File = new File(File.applicationDirectory.resolvePath("upload.log").nativePath);
				_stream = new FileStream();
				_stream.open(logFile, FileMode.APPEND);

				var df:DateFormatter = new DateFormatter();
				df.formatString = "YYYY-MM-DD HH:NN:SS";
				_stream.writeUTFBytes("程序启动：" + df.format(new Date()) + "\r\n");

				_inited = true;
			}
			catch(err:Error){
				trace(err);
				_inited = false;
			}
		}

		public static function log(info:String):void {
			if(!_inited) init();
			if(_stream == null) return;

			_stream.writeUTFBytes(info + "\r\n");
		}

		public static function release():void {
			if(!_inited || _stream == null) return;

			_stream.writeUTFBytes("--------------------------------------------------\r\n\r\n");
			_stream.close();
		}
	}
}